/*
 * Main.c
 *
 *  Created on: 16 juli 2021
 *      Author: Daniel Mårtensson
 */

#include <stdio.h>

/* Include Open SAE J1939 */
#include "Open_SAE_J1939/Open_SAE_J1939.h"

/* Include ISO 11783 */
#include "ISO_11783/ISO_11783-7_Application_Layer/Application_Layer.h"

int main() {

	/* Create our J1939 structure with two ECU */
	J1939 j1939_1 = {0};
	J1939 j1939_2 = {0};

	/* Important to sent all non-address to 0xFF - Else we cannot use ECU address 0x0 */
	uint8_t i;
	for(i = 0; i < 255; i++){
		j1939_1.other_ECU_address[i] = 0xFF;
		j1939_2.other_ECU_address[i] = 0xFF;
	}

	/* Set the ECU address */
	j1939_1.information_this_ECU.this_ECU_address = 0x80;												/* From 0 to 253 because 254 = error address and 255 = broadcast address */
	j1939_2.information_this_ECU.this_ECU_address = 0x90;

	/* Set the information about valve 1 for ECU 2 */
	uint8_t valve_number = 1;
	j1939_2.this_auxiliary_valve_estimated_flow[valve_number].extend_estimated_flow_standard = 100;
	j1939_2.this_auxiliary_valve_estimated_flow[valve_number].fail_safe_mode = FAIL_SAFE_MODE_BLOCKED;	/* No fail safe mode */
	j1939_2.this_auxiliary_valve_estimated_flow[valve_number].limit = LIMIT_NOT_USED;
	j1939_2.this_auxiliary_valve_estimated_flow[valve_number].retract_estimated_flow_standard = 50;
	j1939_2.this_auxiliary_valve_estimated_flow[valve_number].valve_state = VALVE_STATE_EXTEND;

	/* Send a request from ECU 1 to ECU 2 */
	ISO_11783_Send_Request_Auxiliary_Valve_Estimated_Flow(&j1939_1, 0x90, valve_number);

	/* Read the request for ECU 2*/
	Open_SAE_J1939_Listen_For_Messages(&j1939_2);

	/* Read the response request for ECU 1 */
	Open_SAE_J1939_Listen_For_Messages(&j1939_1);

	/* Display what we got */
	printf("Extend estimated flow standard = %i\nFail safe mode = %i\nLimit = %i\nRetract estimated flow standard = %i\nValve state = %i\nFrom ECU address = 0x%X"
			,j1939_1.from_other_ecu_auxiliary_valve_estimated_flow[valve_number].extend_estimated_flow_standard
			,j1939_1.from_other_ecu_auxiliary_valve_estimated_flow[valve_number].fail_safe_mode
			,j1939_1.from_other_ecu_auxiliary_valve_estimated_flow[valve_number].limit
			,j1939_1.from_other_ecu_auxiliary_valve_estimated_flow[valve_number].retract_estimated_flow_standard
			,j1939_1.from_other_ecu_auxiliary_valve_estimated_flow[valve_number].valve_state
			,j1939_1.from_other_ecu_auxiliary_valve_estimated_flow[valve_number].from_ecu_address);

	return 0;
}
